package com.fangngng.pushall.controller;

import com.fangngng.pushall.VO.CenterVO;
import com.fangngng.pushall.constant.ErrorCode;
import com.fangngng.pushall.dto.Result;
import com.fangngng.pushall.dto.TagQuery;
import com.fangngng.pushall.dto.TopicQuery;
import com.fangngng.pushall.entity.Tag;
import com.fangngng.pushall.entity.Topic;
import com.fangngng.pushall.entity.User;
import com.fangngng.pushall.service.TagService;
import com.fangngng.pushall.service.TopicService;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 个人中心
 * @author fei.fang
 * @date 2020/10/29
 **/
@Slf4j
@RestController
public class CenterController extends BaseController{

    @Autowired
    private TopicService topicService;

    @Autowired
    private TagService tagService;

    private static final Gson gson = new Gson();

    /**
     * 个人中心
     * @return
     */
    @GetMapping(value = "/center")
    @PreAuthorize("isAuthenticated()")
    public Result<CenterVO> center(){
        User userDetail = super.getCurrentUser();
        if (userDetail == null){
            log.warn("dashboard需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }
        log.info("进入个人中心, user:{}", gson.toJson(userDetail));

        // 个人创建的主题
        TopicQuery topicQuery = new TopicQuery();
        topicQuery.setCreateEmail(userDetail.getEmail());
        PageInfo<Topic> topicPageInfo = topicService.getTopicByPage(1,10,"create_time desc ",topicQuery);

        // 个人订阅的主题
        PageInfo<Topic> subTopicPageInfo = topicService.getSubTopicByPage(1,10,"create_time desc ",topicQuery);

        CenterVO centerVO = new CenterVO();
        centerVO.setUserDetail(userDetail);
        centerVO.setTopicPageInfo(topicPageInfo);
        centerVO.setSubTopicPageInfo(subTopicPageInfo);

        return Result.ok(centerVO);
    }

    /**
     * 个人创建的主题
     * 分页
     * @return
     */
    @PostMapping(value = "/center/topic/create")
    @PreAuthorize("isAuthenticated()")
    public Result<PageInfo<Topic>> topicCreate(){
        User userDetail = super.getCurrentUser();
        if (userDetail == null){
            log.warn("dashboard需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }

        TopicQuery topicQuery = new TopicQuery();
        topicQuery.setCreateEmail(userDetail.getEmail());
        PageInfo<Topic> topicPageInfo = topicService.getTopicByPage(1,10,"create_time desc ",topicQuery);

        return Result.ok(topicPageInfo);
    }

    /**
     * 个人订阅的主题
     * @return
     */
    @GetMapping(value = "/center/topic/subscribe")
    @PreAuthorize("isAuthenticated()")
    public Result<PageInfo<Topic>> subscribe(){
        User userDetail = super.getCurrentUser();
        if (userDetail == null){
            log.warn("dashboard需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }

        TopicQuery topicQuery = new TopicQuery();
        topicQuery.setSubscribeEmail(userDetail.getEmail());
        PageInfo<Topic> topicPageInfo = topicService.getSubTopicByPage(1,10,"create_time desc ",topicQuery);

        return Result.ok(topicPageInfo);
    }

    /**
     * 个人订阅的标签
     * @return
     */
    @GetMapping(value = "/center/tag/subscribe")
    @PreAuthorize("isAuthenticated()")
    public Result<PageInfo<Tag>> subscribeTags(){
        User userDetail = super.getCurrentUser();
        if (userDetail == null){
            log.warn("dashboard需要登录");
            return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
        }

        TagQuery topicQuery = new TagQuery();
        topicQuery.setSubscribeEmail(userDetail.getEmail());
        PageInfo<Tag> topicPageInfo = tagService.getSubTagByPage(1,10,"create_time desc ", topicQuery);

        return Result.ok(topicPageInfo);
    }

}
